/*
 * sun_Net_IndItem
 * 独立物品
 * 
 */

//为了确认有没有改漏先将原来的全部删掉
Game_Party.prototype.initAllItems = function () {
    // this._items = {};
    // this._weapons = {};
    // this._armors = {};
}

class Net_Item {
    constructor(data) {
        this._type = "";
        this._id = 0;
        this._tempSeed = 0;
        this._num = 1;
        this._appraisal = [];
        this._stackable = false;
        this._equipped = false;
        if (data) {
            this.setItem(data);
            this.makeParams();
        }
    }

    setItem(data) {
        const { type, id, paramSeed, appraisalSeed } = data;
        this._type = type;
        this._id = id;
        this._paramSeed = paramSeed;
        this._appraisalSeed = appraisalSeed;
    }

    //装备属性index
    //0:mhp ,1:mmp , 2:atk , 3:def , 4:mat , 5:mdf , 6:agi , 7:luk

    makeParams() {
        if (this._type === "weapon" || this._type === "armor") {
            this.iniParam();
            this.makeBaseParam();
            this.makeVaryingParam();
            this.setAppraisalParam();
            this.makeAppraisalParam();
        }
    }

    iniParam() {
        this._params = [0, 0, 0, 0, 0, 0, 0, 0];
        this._paramRates = [0, 0, 0, 0, 0, 0, 0, 0];
        this._appraisal = [];
    }

    makeBaseParam() {
        const data = this.object();
        for (let i = 0; i < data.params.length; i++) {
            this._params[i] += data.params[i];
        }
    }

    makeVaryingParam() {
        const data = this.object();
        if (data.meta.Params) {
            const paramArr = data.meta.Params.split(",");
            const valueArr = data.meta.ParamsValue.split(",").map(Number);
            const rateArr = data.meta.ParamsRate.split(",").map(Number);
            this._tempSeed = this._paramSeed;
            for (let i = 0; i < paramArr.length; i++) {
                this.addParam(paramArr[i], valueArr[i], rateArr[i]);
            }
        }
    }

    setAppraisalParam() {
        const data = this.object();
        const noteNum = data.meta.AppraisalNum;
        if (noteNum) {
            const num = Number(noteNum);
            for (let i = 0; i < num; i++) {
                this._appraisal.push(null);
            }
        }
    }

    appraisal(seed) {
        this._appraisalSeed = seed;
        this.makeParams();

    }

    makeAppraisalParam() {
        if (!this._appraisalSeed) return;
        const data = this.object();
        if (data.meta["A-Params"]) {
            const paramArr = data.meta["A-Params"].split(",");
            const valueArr = data.meta["A-ParamsValue"].split(",").map(Number);
            const rateArr = data.meta["A-ParamsRate"].split(",").map(Number);
            const num = paramArr.length;
            this._tempSeed = this._appraisalSeed;
            for (let i = 0; i < this._appraisal.length; i++) {
                const index = Math.floor(num * this.random());
                const param = paramArr[index];
                this.addParam(param, valueArr[index], rateArr[index]);
                this._appraisal[i] = param;
            }
        }
    }

    addParam(param, value, rate) {
        let valueArr;
        if (param.charAt(param.length - 1) === "%") {
            valueArr = this._paramRates;
            param = param.substr(0, param.length - 1);
        } else {
            valueArr = this._params;
        }
        switch (param) {
            case "mhp":
                valueArr[0] += Math.round(value * (1 + this.random() * rate));
                break;
            case "mmp":
                valueArr[1] += Math.round(value * (1 + this.random() * rate));
                break;
            case "atk":
                valueArr[2] += Math.round(value * (1 + this.random() * rate));
                break;
            case "def":
                valueArr[3] += Math.round(value * (1 + this.random() * rate));
                break;
            case "mat":
                valueArr[4] += Math.round(value * (1 + this.random() * rate));
                break;
            case "mdf":
                valueArr[5] += Math.round(value * (1 + this.random() * rate));
                break;
            case "agi":
                valueArr[6] += Math.round(value * (1 + this.random() * rate));
                break;
            case "luk":
                valueArr[7] += Math.round(value * (1 + this.random() * rate));
                break;
            default:
                break;
        }
    }

    random() {
        this._tempSeed = (this._tempSeed * 9301 + 49297) % 233280;
        return this._tempSeed / 233280.0;
    }

    object() {
        if (this.isItem()) {
            return $dataItems[this._id];
        } else if (this.isWeapon()) {
            return $dataWeapons[this._id];
        } else if (this.isArmor()) {
            return $dataArmors[this._id];
        } else {
            return null;
        }
    }

    isItem() {
        return this._type === "item";
    }

    isWeapon() {
        return this._type === "weapon";
    }

    isArmor() {
        return this._type === "armor";
    }
}

Game_Party.prototype.allItems = function () {
    return NetManager.item;
}

/* 物品 */
Game_Party.prototype.items = function () {
    return this.allItems().filter(item => item.isItem());
}

/* 武器 */
Game_Party.prototype.weapons = function () {
    return this.allItems().filter(item => item.isWeapon());
}

/* 防具 */
Game_Party.prototype.armors = function () {
    return this.allItems().filter(item => item.isArmor());
}

/* 装备物品（武器和防具） */
Game_Party.prototype.equipItems = function () {
    return this.weapons().concat(this.armors());
}

Game_Party.prototype.seekItem = function (item) {
    for (const it of this.allItems()) {
        if (it._type === item._type && it._id === item._id) {
            return it;
        }
    }
    return false;
}

/* 物品数量 */
Game_Party.prototype.numItems = function (item) {
    let num = 0;
    for (const it of this.allItems()) {
        if (it._type === item._type && it._id === item._id) {
            num += it.num;
        }
    }
    return num;
}

/* 获得物品 */
Game_Party.prototype.gainItem = function (item, amount, includeEquip) {
    if (amount <= 0) return;
    //判断物品是否可堆叠
    if (item._stackable) {
        //查找是否已有相同物品
        const it = this.seekItem(item);
        if (it) {
            it._num += amount;
        } else {
            this.allItems().push(item);
        }
    } else {
        this.allItems().push(item);
    }
    $gameMap.requestRefresh();
}

/* 失去物品 */
Game_Party.prototype.loseItem = function (item, amount, includeEquip) {
    if (amount >= 0) return false;
    if (this.numItems(item) < amount) return false;
    const container = this.allItems();
    //判断物品是否可堆叠
    if (item._stackable) {
        for (let i = container.length - 1; i >= 0; i--) {
            if (container[i]._type === item._type && container[i]._id === item._id) {
                if (container[i]._num > amount) {
                    container[i]._num -= amount;
                    return true;
                } else if (container[i]._num === amount) {
                    container[i].splice(i, 1);
                    return true;
                } else {
                    amount -= container[i]._num;
                    container[i].splice(i, 1);
                }
            }
        }
    }
    for (let i = container.length - 1; i >= 0; i--) {
        if (container[i]._type === item._type && container[i]._id === item._id) {
            container.splice(i, 1);
            if (--amount <= 0) return true;
        }
    }
}

/* 设置战斗测试物品 */
Game_Party.prototype.setupBattleTestItems = function () {
    // for (const item of $dataItems) {
    //     if (item && item.name.length > 0) {
    //         this.gainItem(item, this.maxItems(item));
    //     }
    // }
}



/* 物品上限 */
Game_Party.prototype.maxItems = function (/*item*/) {
    return 99;
};

/* 是否该物品数量超过上限*/
Game_Party.prototype.hasMaxItems = function (item) {
    return this.numItems(item) >= this.maxItems(item);
};

/* 是否拥有该物品 */
Game_Party.prototype.hasItem = function (item, includeEquip) {
    if (this.numItems(item) > 0) {
        return true;
    } else if (includeEquip && this.isAnyMemberEquipped(item)) {
        return true;
    } else {
        return false;
    }
};

/* 是否所有成员装备该物品 */
Game_Party.prototype.isAnyMemberEquipped = function (item) {
    return this.members().some(actor => actor.equips().includes(item));
};



/* 丢弃成员装备 */
Game_Party.prototype.discardMembersEquip = function (item, amount) {
    let n = amount;
    for (const actor of this.members()) {
        while (n > 0 && actor.isEquipped(item)) {
            actor.discardEquip(item);
            n--;
        }
    }
};

;

/* 消耗物品 */
Game_Party.prototype.consumeItem = function (item) {
    if (DataManager.isItem(item) && item.consumable) {
        this.loseItem(item, 1);
    }
};

/* 是否可使用 */
Game_Party.prototype.canUse = function (item) {
    return this.members().some(actor => actor.canUse(item));
};